package com.estar.jinyou.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.estar.jinyou.vo.JInYouRequestVO;
import com.estar.jinyou.vo.JYReturnVO;
import com.estar.jinyou.vo.voEntity.EvalAssistInfoVO;
import com.estar.jinyou.vo.voEntity.EvalPartInfoVO;
import com.estar.jinyou.vo.voEntity.EvalRepairInfoVO;

import net.sf.json.JSONObject;

@Controller("jinYou")
@Scope("prototype")
public class jinYou {
	
	private java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final Logger logger = Logger.getLogger(jinYou.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	private JinYouController jc;
	
	@RequestMapping(params = "getInfo")
	public void getInfo(HttpServletRequest request,HttpServletResponse response){
		JYReturnVO  jyR=new JYReturnVO();
		JInYouRequestVO jy=new JInYouRequestVO();	
		InputStreamReader isr = null;
		BufferedReader in =null;
		try{
			isr = new InputStreamReader(request.getInputStream(), "UTF-8");
			in = new BufferedReader(isr);
			StringBuffer inStr = new StringBuffer("");
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				inStr.append(inputLine);
				inStr.append("\n");
			}
			in.close();
			
			if (inStr == null || "".equals(inStr.toString())) {
				jyR.setMessage("接收的数据为空");
				jyR.setCode("0");
			} else {
				String mess=URLDecoder.decode(inStr.toString(), HTTP.UTF_8);
				//logger.info("精友===调用接口于"+df.format(new Date())+";传送的内容============"+inStr.toString());
				logger.info("精友===调用接口于"+df.format(new Date())+";解析传送的内容============"+mess);
				
				JSONObject jsonObject = JSONObject.fromObject(mess);
				
				Map<String, Class> classMap = new HashMap<String, Class>(); 
				classMap.put("evalPartInfoList", EvalPartInfoVO.class);  
			    classMap.put("evalRepairList", EvalRepairInfoVO.class);  
			    classMap.put("evalAssitList", EvalAssistInfoVO.class);
			 
				jy=(JInYouRequestVO) JSONObject.toBean(jsonObject,JInYouRequestVO.class,classMap);
				
				
				String name=jy.getUsecode();
				String pass=jy.getPassword();
				String sql="select id from tmbl_evalresponse s where s.state='1' and s.USECODE='"+name+"' and s.PASSWORD='"+pass+"'";
				List<String> idList=this.systemService.findListbySql(sql);
				if(idList!=null && idList.size()>0){
					jyR=this.jc.doWork(jy);
				}else{
					jyR.setMessage("用户不存在");
					jyR.setCode("0");
				}
			}
			
		}catch(Exception e){
			logger.error("精友的系统错误",e);
		}finally {
			String outStr = JSONObject.fromObject(jyR).toString();
			logger.info("调用接口于"+df.format(new Date())+";返回内容============"+outStr);
			
			try {
				if(in!=null){
					in.close();
				}
				if(isr!=null){
					isr.close();
				}
				
				response.setContentType("text/html; charset=GB2312");
				PrintWriter write = response.getWriter();
				write.write(outStr);
				write.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
